package demo;

import hcsdk.HCNetSDK;
import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;
import org.jdesktop.application.Action;

import javax.swing.*;

/**
 * @author yangxiaofeng6
 */
public class NetworkSetupBox extends javax.swing.JDialog {
    public static HCNetSDK hCNetSDK = HCNetSDK.INSTANCE;
    public HCNetSDK.NET_DVR_NETCFG_V50 m_struNetCfg = new HCNetSDK.NET_DVR_NETCFG_V50();
    public AlarmJavaDemoView m_alarm;

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton NetworkCfgGetButton;
    private javax.swing.JButton NetworkSetupCloseButton;
    private javax.swing.JTextPane NetworkSetupIP;
    private javax.swing.JTextPane NetworkSetupPort;
    private javax.swing.JButton NetworkSetupSetButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;

    /**
     * Creates new form NetworkSetupBox
     */
    public NetworkSetupBox(java.awt.Frame parent, AlarmJavaDemoView obj) {
        super(parent);
        initComponents();

        m_alarm = obj;
        Init();
    }

    public void Init() {
        IntByReference dwReturned = new IntByReference(0);
        NativeLong lChannel = new NativeLong(0);

        Pointer lpNetCfg = m_struNetCfg.getPointer();

        if (hCNetSDK.NET_DVR_GetDVRConfig(m_alarm.lUserID, HCNetSDK.NET_DVR_GET_NETCFG_V50, lChannel, lpNetCfg, m_struNetCfg.size(), dwReturned)) {
            JOptionPane.showMessageDialog(null, "NET_DVR_GET_NETCFG_V50 true");
        } else {
            int code = hCNetSDK.NET_DVR_GetLastError();
            JOptionPane.showMessageDialog(null, "NET_DVR_GET_NETCFG_V50 failed: " + code);
            return;
        }
        m_struNetCfg.read();
        String ip = new String(m_struNetCfg.struEtherNet[0].struDVRIP.sIpV4);
        NetworkSetupIP.setText(ip);
        String port = new String(String.valueOf(m_struNetCfg.struEtherNet[0].wDVRPort));
        NetworkSetupPort.setText(port);
    }

    @Action
    public void CleseBox() {
        dispose();
    }

    @Action
    public void NetworkSetupSetButtonClick() {
        String ip = NetworkSetupIP.getText();
        String port = NetworkSetupPort.getText();

        m_struNetCfg.struEtherNet[0].struDVRIP.sIpV4 = new byte[16];
        for (int i = 0; i < 16; i++) {
            m_struNetCfg.struEtherNet[0].struDVRIP.sIpV4[i] = 0;
        }
        m_struNetCfg.struEtherNet[0].struDVRIP.sIpV4 = ip.getBytes();
        m_struNetCfg.struEtherNet[0].wDVRPort = (short) Integer.parseInt(port);

        NativeLong lChannel = new NativeLong(0);

        m_struNetCfg.write();
        Pointer lpNetCfg = m_struNetCfg.getPointer();

        if (hCNetSDK.NET_DVR_SetDVRConfig(m_alarm.lUserID, HCNetSDK.NET_DVR_SET_NETCFG_V50, lChannel, lpNetCfg, m_struNetCfg.size())) {
            JOptionPane.showMessageDialog(null, "NET_DVR_SET_NETCFG_V50 true");
        } else {
            int code = hCNetSDK.NET_DVR_GetLastError();
            JOptionPane.showMessageDialog(null, "NET_DVR_SET_NETCFG_V50 failed");
            return;
        }

    }

    @Action
    public void networkGetButtonClick() {
        Init();
    }

    /**
     * This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        NetworkCfgGetButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        NetworkSetupIP = new javax.swing.JTextPane();
        NetworkSetupCloseButton = new javax.swing.JButton();
        NetworkSetupSetButton = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        NetworkSetupPort = new javax.swing.JTextPane();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(AlarmJavaDemoApp.class).getContext().getActionMap(NetworkSetupBox.class, this);
        NetworkCfgGetButton.setAction(actionMap.get("networkGetButtonClick")); // NOI18N
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(AlarmJavaDemoApp.class).getContext().getResourceMap(NetworkSetupBox.class);
        NetworkCfgGetButton.setText(resourceMap.getString("NetworkCfgGetButton.text")); // NOI18N
        NetworkCfgGetButton.setName("NetworkCfgGetButton"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        NetworkSetupIP.setName("NetworkSetupIP"); // NOI18N
        jScrollPane1.setViewportView(NetworkSetupIP);

        NetworkSetupCloseButton.setAction(actionMap.get("CleseBox")); // NOI18N
        NetworkSetupCloseButton.setText(resourceMap.getString("NetworkSetupCloseButton.text")); // NOI18N
        NetworkSetupCloseButton.setName("NetworkSetupCloseButton"); // NOI18N

        NetworkSetupSetButton.setAction(actionMap.get("NetworkSetupSetButtonClick")); // NOI18N
        NetworkSetupSetButton.setText(resourceMap.getString("NetworkSetupSetButton.text")); // NOI18N
        NetworkSetupSetButton.setName("NetworkSetupSetButton"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        NetworkSetupPort.setName("NetworkSetupPort"); // NOI18N
        jScrollPane2.setViewportView(NetworkSetupPort);

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addGap(71, 71, 71).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jLabel2).addComponent(jLabel1)).addGap(27, 27, 27).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER).addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE).addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)).addGap(92, 92, 92)).addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addContainerGap(191, Short.MAX_VALUE).addComponent(NetworkCfgGetButton).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addComponent(NetworkSetupSetButton, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE).addGap(14, 14, 14).addComponent(NetworkSetupCloseButton).addContainerGap()));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addGap(79, 79, 79).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING).addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(jLabel1)).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING).addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(jLabel2)).addGap(92, 92, 92).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(NetworkSetupSetButton).addComponent(NetworkSetupCloseButton).addComponent(NetworkCfgGetButton)).addContainerGap()));

        pack();
    }// </editor-fold>//GEN-END:initComponents
    // End of variables declaration//GEN-END:variables

}
